#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BOARDSIZE 256





/*
Read testboards.txt and call the isWin function for each board and compare the output.
*/
int main(void)
{
	char *board, value[4];
	int points = 0;
	int ret = 0;
	FILE *file, *in;
	int i = 0;
//	FILE *popen();
	char buff[32];
	
	file = fopen("testboards.txt","r"); //create file and or write to it
	
	board = (char *)calloc(BOARDSIZE, sizeof(char));
	
	
	
	do{
		
		fscanf(file,"%s",board);
		fgetc(file);//get the space - leave it out
		
		i = 0;
		value[i] = fgetc(file);//get first char
		
		if(value[i] == '-')//if it is a negative sign, get the next one
		{
			i++;
			value[i] = fgetc(file);//get second char
			i++;
			value[i] = '\0';//mark the end
						points = ('0' - value[1]);
			
		}
		
		else
		{
			value[1] = '\0';//mark the end
			points = 0 - ('0' - value[0]);
			
		}
		
		
		
		//printf("%d\n\n", points);
		
		
	 	/* popen creates a pipe so we can read the output
	  	of the program we are invoking */
	  
     		if (!(in = popen("isWin", "r"))) {
     			printf("pipe problem.");
     			exit(1);
     		}
     		fprintf( stdout, "%s\n", board);//give the program some output?
     		fflush(in);
     		
     		fgets(buff, sizeof(buff), in);
     		//ret = atoi(buff);
     		
     		printf("isWin returned: %s, Test value is: %d\n", buff, points);
     		/*
     		if(ret == points)
     			printf("Test PASS\n");
     		else
     			printf("Test *************FAIL**************\n");
     			*/
     		
	}while (!feof(file));

	fclose(file); /*done!*/
	return 0;
}
